package cn.iocoder.yudao.module.mqtt.controller.admin.devicelatestlog;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.mqtt.controller.admin.devicelatestlog.vo.*;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.devicelatestlog.DeviceLatestLogDO;
import cn.iocoder.yudao.module.mqtt.service.devicelatestlog.DeviceLatestLogService;

@Tag(name = "管理后台 - 数据上传")
@RestController
@RequestMapping("/mqtt/device-latest-log")
@Validated
public class DeviceLatestLogController {

    @Resource
    private DeviceLatestLogService deviceLatestLogService;

    @PostMapping("/create")
    @Operation(summary = "创建数据上传")
    @PreAuthorize("@ss.hasPermission('mqtt:device-latest-log:create')")
    public CommonResult<Long> createDeviceLatestLog(@Valid @RequestBody DeviceLatestLogSaveReqVO createReqVO) {
        return success(deviceLatestLogService.createDeviceLatestLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据上传")
    @PreAuthorize("@ss.hasPermission('mqtt:device-latest-log:update')")
    public CommonResult<Boolean> updateDeviceLatestLog(@Valid @RequestBody DeviceLatestLogSaveReqVO updateReqVO) {
        deviceLatestLogService.updateDeviceLatestLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据上传")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mqtt:device-latest-log:delete')")
    public CommonResult<Boolean> deleteDeviceLatestLog(@RequestParam("id") Long id) {
        deviceLatestLogService.deleteDeviceLatestLog(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除数据上传")
                @PreAuthorize("@ss.hasPermission('mqtt:device-latest-log:delete')")
    public CommonResult<Boolean> deleteDeviceLatestLogList(@RequestParam("ids") List<Long> ids) {
        deviceLatestLogService.deleteDeviceLatestLogListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据上传")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mqtt:device-latest-log:query')")
    public CommonResult<DeviceLatestLogRespVO> getDeviceLatestLog(@RequestParam("id") Long id) {
        DeviceLatestLogDO deviceLatestLog = deviceLatestLogService.getDeviceLatestLog(id);
        return success(BeanUtils.toBean(deviceLatestLog, DeviceLatestLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据上传分页")
    @PreAuthorize("@ss.hasPermission('mqtt:device-latest-log:query')")
    public CommonResult<PageResult<DeviceLatestLogRespVO>> getDeviceLatestLogPage(@Valid DeviceLatestLogPageReqVO pageReqVO) {
        PageResult<DeviceLatestLogDO> pageResult = deviceLatestLogService.getDeviceLatestLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceLatestLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据上传 Excel")
    @PreAuthorize("@ss.hasPermission('mqtt:device-latest-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceLatestLogExcel(@Valid DeviceLatestLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceLatestLogDO> list = deviceLatestLogService.getDeviceLatestLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数据上传.xls", "数据", DeviceLatestLogRespVO.class,
                        BeanUtils.toBean(list, DeviceLatestLogRespVO.class));
    }

}